package com.link.play.fit.ai.ui.main.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.link.play.fit.ai.R
import com.link.play.fit.ai.bean.ControlDayData
import com.link.play.fit.ai.databinding.ControlListItemLayoutBinding
import com.link.play.fit.ai.ext.d

class DayItemAdapter(
    private val pageData: List<ControlDayData>,
    private val page: Int,
    private val context: Context,
    private val onclick: (Int) -> Unit
) :
    RecyclerView.Adapter<DayItemAdapter.DayViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DayViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.control_list_item_layout, parent, false)
        return DayViewHolder(view)
    }


    override fun onBindViewHolder(
        holder: DayViewHolder,
        @SuppressLint("RecyclerView") position: Int
    ) {
        holder.itemBinding.itemNameText.text = pageData[position].timeValue
        holder.itemBinding.itemNameText.setOnClickListener {
            if(pageData[position].isAfter) return@setOnClickListener
            onclick.invoke(position)
//            when (page) {
//                0 -> {
//                    onclick.invoke(position)
//                }
//                1 -> {
//                    onclick.invoke(position + 7)
//                }
//                2 -> {
//                    onclick.invoke(position + 14)
//                }
//                3 -> {
//                    onclick.invoke(position + 21)
//                }
//                4 -> {
//                    onclick.invoke(position + 28)
//                }
//            }
//            notifyDataSetChanged()

        }
        if(pageData[position].isAfter){
            holder.itemBinding.itemNameText.setTextColor(context.getColor(R.color.color_69))
        }else{
            if (position == pageData[position].currentPosition % 7) {
                holder.itemBinding.itemNameText.isSelected = true
//                holder.itemBinding.itemNameText.background = context.getDrawable(R.drawable.bg_style_d9ebff_r8)
            } else {
//                holder.itemBinding.itemNameText.background = null
                holder.itemBinding.itemNameText.isSelected = false
            }
        }

    }

    override fun getItemCount() = pageData.size

    inner class DayViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var itemBinding: ControlListItemLayoutBinding =
            ControlListItemLayoutBinding.bind(itemView)
    }
}
